#!/usr/bin/env bash
# bash <(curl -sLk https://gitee.com/uon/script/raw/master/shell/acme_install.sh)

tmp_path='/tmp/07DA0AE5-D7A3-41AE-FA9D-185402F1C2DB'

function Download() {
    local downloadUrl githubMirrors toFile

    downloadUrl=$(echo "$1" | sed -r "s|^(https?\:\/\/)?github.com\/(.*)(\.zip$)|\2.tar.gz|gi")

    [ ! -d "${tmp_path}" ] && mkdir "${tmp_path}"
    toFile=${tmp_path}/${downloadUrl##*/}

    githubMirrors=(
        https://github.com.cnpmjs.org/
        https://github.com/
    )

    for i in "${githubMirrors[@]}"; do
        if curl -fL --connect-timeout 3 --retry 3 "$i$downloadUrl" -o "$toFile"; then
            break
        fi

    done
}

url='https://github.com/acmesh-official/acme.sh/archive/refs/heads/master.zip'

Download $url

cd ${tmp_path}/ || exit
tar -zxvf master.tar.gz

cd acme.sh-master || exit
bash acme.sh --install --force -m

rm -rf ${tmp_path}
